package com.chuanye.jiguangimdemo.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.activitys.ChangePasswordActivity;
import com.chuanye.jiguangimdemo.activitys.LoginActivity;
import com.chuanye.jiguangimdemo.activitys.PersonalActivity;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * 我的
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {
    private static final String TAG = "MineFragment";
    private Context mContext;

    private LinearLayout mine_personage_ll;
    private TextView mine_personage_nickname_tv;
    private ImageView mine_personage_photo_iv;
    private TextView mine_personage_signature_tv;
    private LinearLayout mine_changepassword_ll;
    private ToggleButton mine_nodisturbing_tbtn;
    private LinearLayout mine_feedback_ll;
    private LinearLayout mine_about_ll;
    private Button mine_logout_btn;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine,null);
        mContext = getActivity();


        initView(view);

        initData();

        return view;
    }



    private void initView(View view) {
        //个人信息
        mine_personage_ll = view.findViewById(R.id.mine_personage_ll);
        mine_personage_ll.setOnClickListener(this);
        mine_personage_nickname_tv = view.findViewById(R.id.mine_personage_nickname_tv);
        mine_personage_photo_iv = view.findViewById(R.id.mine_personage_photo_iv);
        mine_personage_signature_tv = view.findViewById(R.id.mine_personage_signature_tv);
        //修改密码
        mine_changepassword_ll = view.findViewById(R.id.mine_changepassword_ll);
        mine_changepassword_ll.setOnClickListener(this);
        //免打扰
        mine_nodisturbing_tbtn = view.findViewById(R.id.mine_nodisturbing_tbtn);
        mine_nodisturbing_tbtn.setOnClickListener(this);

        //反馈
        mine_feedback_ll = view.findViewById(R.id.mine_feedback_ll);
        mine_feedback_ll.setOnClickListener(this);
        //关于
        mine_about_ll = view.findViewById(R.id.mine_about_ll);
        mine_about_ll.setOnClickListener(this);
        //退出登录
        mine_logout_btn = view.findViewById(R.id.mine_logout_btn);
        mine_logout_btn.setOnClickListener(this);

    }

    private void initData() {
        //获取用户信息
        //异步从后台获取用户信息，此接口可用来获取不同appkey下用户的信息,如果appKey为空，
        // 则默认获取当前appkey下的用户信息。
        JMessageClient.getUserInfo("cyq1", "", new GetUserInfoCallback(){

            @Override
            public void gotResult(int responseCode, String responseMessage, UserInfo userInfo) {
                Log.i(TAG,"获取用户信息：返回码："+responseCode+" ，返回数据："+responseMessage);
                if (userInfo != null){
                    Log.i(TAG,"获取用户信息：getUserInfo用户名："+userInfo.getUserName()+" ,别名："+userInfo.getNickname());
                }

            }
        });

        //获取当前登录账号的用户信息
        //此接口会直接从本地返回当前已经登录的用户的信息。
        UserInfo userInfo = JMessageClient.getMyInfo();
        Log.i(TAG,"获取用户信息：getMyInfo 用户名："+userInfo.getUserName()+" ,别名："+userInfo.getNickname());


        //用户名
        if (!TextUtils.isEmpty(userInfo.getUserName())){
            mine_personage_nickname_tv.setText(userInfo.getUserName());
        }
        if (!TextUtils.isEmpty(userInfo.getSignature())){
            mine_personage_signature_tv.setText(userInfo.getSignature());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mine_personage_ll://个人信息
                Intent personageintent = new Intent(mContext, PersonalActivity.class);
                startActivity(personageintent);
                break;
            case R.id.mine_changepassword_ll://修改密码
                Intent changepasswordintent = new Intent(mContext, ChangePasswordActivity.class);
                startActivity(changepasswordintent);

                break;
            case R.id.mine_nodisturbing_tbtn://免打扰

                break;
            case R.id.mine_feedback_ll://反馈

                break;
            case R.id.mine_about_ll://关于

                break;
            case R.id.mine_logout_btn://退出登录
                JMessageClient.logout();
                Intent intent = new Intent(mContext, LoginActivity.class);
                mContext.startActivity(intent);
                getActivity().finish();
                break;

        }
    }



}
